package com.loong.musicdemo;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.loong.musicdemo.model.MV;
import com.loong.musicdemo.model.Song;
import com.loong.musicdemo.model.SongList;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(LitePal.findAll(Song.class)!=null){
            LitePal.deleteAll(Song.class);
        }
        if(LitePal.findAll(MV.class)!=null){
            LitePal.deleteAll(MV.class);
        }
        if(LitePal.findAll(SongList.class)!=null){
            LitePal.deleteAll(SongList.class);
        }
        setContentView(R.layout.activity_main);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_find, R.id.navigation_video, R.id.navigation_mine, R.id.navigation_cloud_town, R.id.navigation_account)
                .build();
        getSupportActionBar().hide();//隐藏ActionBar
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
        getPermission();
    }
    //获取读写内存的权限
    private void getPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
            // 无权限时去请求权限
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);

        }
    }

    /**
     * 直接在Activity重写 onRequestPermissionsResult()方法即可
     * requestCode requetsPermissions()方法中传入的RequestCode
     * String[] permissons  requetsPermissions()方法中传入的permission数组
     * grantResults  返回的具体结果
     **/
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch(requestCode) {
            case 1:
                if (grantResults.length > 0  && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "请求权限成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "请求权限被拒绝", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

}
